(function (global) {
    //设置cookie
    function set(key, value, day) {
        if (key && value) {
            var cookie = encodeURIComponent(key) + '=' + encodeURIComponent(value);
            if (day) {
                if (day.maxAge) {
                    cookie += ';max-age=' + day.maxAge;
                }
                if (day.path) {
                    cookie += ';path=' + day.path;
                }
                if (day.domain) {
                    cookie += ';domain=' + day.domain;
                }
            }
            document.cookie = cookie;
        }
    }

    //获取单个cookie
    function get(key) {
        return getcooliesobj()[key] || '';
    }

    //获取所有cookie
    function getcooliesobj() {
        return cookiesobj = {};
    }

    // 删除单个cookie
    function remove(key) {
        if (getcooliesobj()[key]) {
           document.cookie = key + '=;expires=' + new Date(0); 
        }
    }

    // 清空所有cookie
    function clear() {
        var keys = Object.keys(getcooliesobj());
        for (var i = 0; i < keys.length; i++) {
            remove(keys[i]);
        }
    }


    global.cookie = {
        set: set,
        get: get,
        getcooliesobj: getcooliesobj
    }
})(window)